/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package giormi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author Ling
 */
public class F_addstock {
    private String s_dateposted;
    private String s_datereceived;
    private String s_quantity;
    private String s_rrnum;
    private String s_quedan;
    private String s_expdate;
    private String s_proddate;
    private String s_lotnum;
    private String s_notes;
    private int s_idproduct;

    public void setS_dateposted(String s_dateposted) {
        this.s_dateposted = s_dateposted;
    }

    public void setS_datereceived(String s_datereceived) {
        this.s_datereceived = s_datereceived;
    }

    public void setS_expdate(String s_expdate) {
        this.s_expdate = s_expdate;
    }

    public void setS_idproduct(int s_idproduct) {
        this.s_idproduct = s_idproduct;
    }

    public void setS_lotnum(String s_lotnum) {
        this.s_lotnum = s_lotnum;
    }

    public void setS_notes(String s_notes) {
        this.s_notes = s_notes;
    }

    public void setS_proddate(String s_proddate) {
        this.s_proddate = s_proddate;
    }

    public void setS_quantity(String s_quantity) {
        this.s_quantity = s_quantity;
    }

    public void setS_quedan(String s_quedan) {
        this.s_quedan = s_quedan;
    }

    public void setS_rrnum(String s_rrnum) {
        this.s_rrnum = s_rrnum;
    }

    private Connection conn;
    private String SQLStatement;
    private String URL;
    private Statement stmt;
    public ResultSet rset;

    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public int getidproduct(String productname, String packing){
        int idproduct = 0;

        connect();
        try {
            SQLStatement = "SELECT idproduct FROM product "
                    + "WHERE p_name='"+productname+"' AND p_packing='"+packing+"' AND p_status ='Active'";
            rset = stmt.executeQuery(SQLStatement);
            if (rset.next()) {
              idproduct = rset.getInt(1);
              System.out.println(rset.getInt(1));
            }
            rset.close();
            disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(F_addstock.class.getName()).log(Level.SEVERE, null, ex);
        }
        return idproduct;
    }

    public void addstock(){
        if (s_datereceived.length()!=8)
            s_datereceived = date.convertDate(s_datereceived);
        if (s_dateposted.length()!=8)
            s_dateposted = date.convertDate(s_dateposted);
        if (s_expdate.length()!=8)
            s_expdate = date.convertDate(s_expdate);
        if (s_proddate.length()!=8)
            s_proddate = date.convertDate(s_proddate);

        try{
           SQLStatement = "INSERT INTO stock "+
                   "(s_balance, s_dateposted, s_datereceived, s_quantity, s_rrnum, s_quedan, "+
                   "s_expdate, s_proddate, s_lotnum, s_notes, s_idproduct) VALUES"+
                   "('"+s_quantity+"','"+s_dateposted+"','"+s_datereceived+"','"+s_quantity+"','"+s_rrnum+"','"+s_quedan+"','"
                   +s_expdate+"','"+s_proddate+"','"+s_lotnum+"','"+s_notes+"','"+s_idproduct+"')";
            stmt.executeUpdate(SQLStatement);
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

}
